package com.example.scaffolding.composer.worker

import android.content.Context
import android.util.Log
import androidx.work.Data
import androidx.work.Worker
import androidx.work.WorkerParameters
import androidx.work.workDataOf
import com.example.scaffolding.composer.KEY_BITMAP_RESULT
import com.example.scaffolding.composer.KEY_RESULT
import com.example.scaffolding.util.ImageLoader

class ImageLoadWorker(var context: Context, workerParameters: WorkerParameters)
    : Worker(context, workerParameters){

    override fun doWork(): Result {

        var url: String? = inputData.getString(KEY_RESULT)
        Log.d(TAG, "url = $url")

        url?.let {
            var bitmapPath: String? = ImageLoader.load(context, it)
            bitmapPath?.let {
                val output: Data = workDataOf(KEY_BITMAP_RESULT to bitmapPath)
                return Result.success(output)
            }
        }

        return Result.success()
    }

    companion object {
        private const val TAG = "ImageLoadWorker"
    }
}